package com.gdsz.libbasic.utils.view

import android.text.InputFilter
import android.text.Spanned
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.widget.EditText
import android.widget.TextView
import java.lang.reflect.Field


class InputMinMaxFilter(private val minValue:Int,private val maxValue:Int,) : InputFilter {
    /**
     * @param source 新输入的字符串
     * @param start  新输入的字符串起始下标，一般为0
     * @param end    新输入的字符串终点下标，一般为source长度-1
     * @param dest   输入之前文本框内容
     * @param dstart 新输入的字符在原字符串中的位置
     * @param dend   原内容终点坐标，
     * @return 输入内容
     */
    override fun filter(
        source: CharSequence,
        start: Int,
        end: Int,
        dest: Spanned,
        dstart: Int,
        dend: Int
    ): CharSequence {
        val sourceText = source.toString()
        val destText = dest.toString()
        if (dstart == 0 && "0" == source) {
            //如果输入是0 且位置在第一位，取消输入
            return ""
        }
        val totalText = StringBuilder()
        totalText.append(destText.substring(0, dstart))
            .append(sourceText)
            .append(destText.substring(dstart, destText.length))
        try {
            //这边设置到多少 这边设置的是到24
            if (totalText.toString().toInt() > maxValue) {
                return ""
            } else if (totalText.toString().toInt() < minValue) {
                return ""
            }
        } catch (e: Exception) {
            return ""
        }
        return if ("" == source.toString()) {
            ""
        } else "" + source.toString().toInt()
    }
}


object EditTextUtil {
    /**
     * 禁止输入框复制粘贴菜单
     */
    fun disableCopyAndPaste(editText: EditText?) {
        try {
            if (editText == null) {
                return
            }
            editText.setOnLongClickListener(View.OnLongClickListener { true })
            editText.isLongClickable = false
            editText.setOnTouchListener(View.OnTouchListener { v, event ->
                if (event.action === MotionEvent.ACTION_DOWN) {
                    // setInsertionDisabled when user touches the view
                    setInsertionDisabled(editText)
                }
                false
            })
            editText.customSelectionActionModeCallback = object : ActionMode.Callback {
                override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                    return false
                }

                override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                    return false
                }

                override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
                    return false
                }

                override fun onDestroyActionMode(mode: ActionMode?) {}
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun setInsertionDisabled(editText: EditText) {
        try {
            val editorField: Field = TextView::class.java.getDeclaredField("mEditor")
            editorField.isAccessible = true
            val editorObject: Any = editorField.get(editText)

            // if this view supports insertion handles
            val editorClass = Class.forName("android.widget.Editor")
            val mInsertionControllerEnabledField: Field =
                editorClass.getDeclaredField("mInsertionControllerEnabled")
            mInsertionControllerEnabledField.isAccessible = true
            mInsertionControllerEnabledField.set(editorObject, false)

            // if this view supports selection handles
            val mSelectionControllerEnabledField: Field =
                editorClass.getDeclaredField("mSelectionControllerEnabled")
            mSelectionControllerEnabledField.isAccessible = true
            mSelectionControllerEnabledField.set(editorObject, false)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}